package com.powernode.controller;


import com.github.pagehelper.Page;
import com.powernode.common.Constants;
import com.powernode.domain.User;
import com.powernode.dto.VisitDto;
import com.powernode.domain.Result;
import com.powernode.domain.VisitLog;

import com.powernode.service.VisitLogService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("visitLog")
public class VisitLogController {

    @Resource
    private VisitLogService visitLogService;

    @GetMapping ("visitCustomer.action")
    public Result visitCustomer(VisitDto visitDto){
        Page<VisitLog> visitLogPage = visitLogService.visitPage(visitDto);

        return new Result(0,"查询成功",visitLogPage.getTotal(),visitLogPage.getResult());
    }

    @PostMapping("addVisitLog.action")
    public Result addVisitLog(VisitLog visitLog, HttpSession session){
        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        visitLog.setUserId(user.getId());
        visitLog.setRealname(user.getRealname());
        try {
            visitLogService.insertSelective(visitLog);
            return new Result(200,"保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"保存失败");
        }
    }

}
